﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Reflection;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
namespace TemplateEngine
{
    /// <summary>
    /// Constructs templates from files
    /// </summary>
    public class FileTemplateFactory : ITemplateFactory
    {
        private DirectoryInfo _source;
        /// <summary>
        /// Constructs a FileTemplateFactory that looks in the specified folder for templates
        /// </summary>
        /// <param name="templateFolder">The folder that contains the templates to load</param>
        public FileTemplateFactory(String templateFolder) : this(new DirectoryInfo(templateFolder))
        {
            Contract.Requires(templateFolder != null);
        }
        /// <summary>
        /// Constructs a FileTemplateFactory that looks in the specified folder for templates
        /// </summary>
        /// <param name="templateFolder">The folder that contains the templates to load</param>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
        public FileTemplateFactory(DirectoryInfo templateFolder)
        {
            Contract.Requires(templateFolder != null);
            _source = templateFolder;
            if (!_source.Exists)
            {
                throw new DirectoryNotFoundException(templateFolder.FullName);
            }
        }

        private Dictionary<String, ITemplate> _cachedTemplates = new Dictionary<string, ITemplate>();
        /// <summary>
        /// Fecthes or Creates a template with the specified name
        /// </summary>
        /// <param name="name">The name of the template to find or create</param>
        /// <returns>The corresponding template or null if none found that match</returns>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
        public ITemplate GetTemplate(String name)
        {
            ITemplate result;
            lock (_cachedTemplates)
            {
                if (!_cachedTemplates.TryGetValue(name, out result))
                {
                    result = LoadTemplate(name);
                    _cachedTemplates.Add(name, result);
                }
            }
            return result;
        }

        private ITemplate LoadTemplate(String name)
        {
            Contract.Requires(name != null);
            FileInfo[] files = _source.GetFiles(name, SearchOption.AllDirectories);
            Contract.Assume(files != null);
            FileInfo file = files.FirstOrDefault();           
            return file != null && file.Exists ? TemplateConverter.loadTemplate(file) : null;
        }

        [ContractInvariantMethod]
        private void AlwaysTrue()
        {
            Contract.Invariant(_source != null);
            Contract.Invariant(_cachedTemplates != null);
        }
    }
}
